home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / AmigaTCP.lha / AmigaTCP / src / timer.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  2KB  |  78 lines

  1. #include "machdep.h"
  2. #include "timer.h"
  3.  
  4. /* Head of running timer chain */
  5. struct timer *timers;
  6.  
  7. tick()
  8. {
  9.     register struct timer *t,*tp;
  10.     register struct timer *expired = NULLTIMER;
  11.  
  12.     /* Run through the list of running timers, decrementing each one.
  13.      * If one has expired, take it off the running list and put it
  14.      * on a singly linked list of expired timers
  15.      */
  16.     for(t = timers;t != NULLTIMER; t = tp){
  17.         tp = t->next;
  18.         if(t->state == TIMER_RUN && --(t->count) == 0){
  19.             stop_timer(t);
  20.             t->state = TIMER_EXPIRE;
  21.             /* Put on head of expired timer list */
  22.             t->next = expired;
  23.             expired = t;
  24.         }
  25.     }
  26.     /* Now go through the list of expired timers, removing each
  27.      * one and kicking the notify function, if there is one
  28.      */
  29.     while((t = expired) != NULLTIMER){
  30.         expired = t->next;
  31.         if(t->func){
  32.             (*t->func)(t->arg);
  33.         }
  34.     }
  35. }
  36. /* Start a timer */
  37. start_timer(t)
  38. register struct timer *t;
  39. {
  40.     char i_state;
  41.  
  42.     if(t == NULLTIMER || t->start == 0)
  43.         return;
  44.     i_state = disable();
  45.     t->count = t->start;
  46.     if(t->state != TIMER_RUN){
  47.         t->state = TIMER_RUN;
  48.         /* Put on head of active timer list */
  49.         t->prev = NULLTIMER;
  50.         t->next = timers;
  51.         if(t->next != NULLTIMER)
  52.             t->next->prev = t;
  53.         timers = t;
  54.     }
  55.     restore(i_state);
  56. }
  57. /* Stop a timer */
  58. stop_timer(t)
  59. register struct timer *t;
  60. {
  61.     char i_state;
  62.  
  63.     if(t == NULLTIMER)
  64.         return;
  65.     i_state = disable();
  66.     if(t->state == TIMER_RUN){
  67.         /* Delete from active timer list */
  68.         if(timers == t)
  69.             timers = t->next;
  70.         if(t->next != NULLTIMER)
  71.             t->next->prev = t->prev;
  72.         if(t->prev != NULLTIMER)
  73.             t->prev->next = t->next;
  74.     }
  75.     t->state = TIMER_STOP;
  76.     restore(i_state);
  77. }
  78.